home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 8
/
Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso
/
Aminet
/
dev
/
c
/
mkid.lha
/
src
/
expand_args.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-29
|
3KB
|
168 lines
/*
** expand_args.c
**
** Expands wild card arguments found in the command line.
** Written by Olaf Barthel (olsen@sourcery.han.de)
** Public Domain
**
** :ts=4
*/
#include <dos/dosextens.h>
#include <dos/dosasl.h>
#include <exec/memory.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LEN 512
typedef struct NameNode
{
struct NameNode *Next;
char *Name;
} *pNameNode, NameNode;
extern struct DosLibrary *DOSBase;
int
expand_args(int argc,char **argv,int *_argc,char ***_argv)
{
NameNode *Root = NULL;
LONG NamePlus = 0;
LONG NameTotal = 0;
LONG Error = 0;
struct AnchorPath *Anchor;
*_argc = argc;
*_argv = argv;
if(DOSBase -> dl_lib . lib_Version < 37)
return(0);
if(Anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath) + MAX_FILENAME_LEN,MEMF_ANY | MEMF_CLEAR))
{
int i;
Anchor -> ap_Strlen = MAX_FILENAME_LEN;
Anchor -> ap_BreakBits = SIGBREAKF_CTRL_C;
for(i = 0 ; !Error && i < argc ; i++)
{
if(i && ParsePatternNoCase(argv[i],Anchor -> ap_Buf,MAX_FILENAME_LEN) == 1)
{
NameNode *Node;
LONG Result;
Result = MatchFirst(argv[i],Anchor);
while(!Result)
{
if(Anchor -> ap_Info . fib_DirEntryType < 0)
{
if(Node = (NameNode *)malloc(sizeof(NameNode) + strlen(Anchor -> ap_Buf) + 1))
{
strcpy(Node -> Name = (char *)(Node + 1),Anchor -> ap_Buf);
Node -> Next = Root;
Root = Node;
NamePlus++;
NameTotal++;
}
else
{
Result = ERROR_NO_FREE_STORE;
break;
}
}
Result = MatchNext(Anchor);
}
if(Result != ERROR_NO_MORE_ENTRIES)
Error = Result;
}
else
{
NameNode *Node;
if(Node = (NameNode *)malloc(sizeof(NameNode)))
{
Node -> Name = argv[i];
Node -> Next = Root;
Root = Node;
NameTotal++;
}
else
Error = ERROR_NO_FREE_STORE;
}
}
if(!Error && NamePlus)
{
char **Index;
if(Index = (char **)malloc(sizeof(char *) * (NameTotal + 1)))
{
NameNode *Node;
*_argc = NameTotal;
*_argv = Index;
Index = &(Index[NameTotal]);
*Index-- = NULL;
Node = Root;
while(Node)
{
*Index-- = Node -> Name;
Node = Node -> Next;
}
}
else
Error = ERROR_NO_FREE_STORE;
}
if(Error || !NamePlus)
{
NameNode *Node,*Next;
Node = Root;
while(Node)
{
Next = Node -> Next;
free(Node);
Node = Next;
}
}
FreeVec(Anchor);
}
else
Error = ERROR_NO_FREE_STORE;
if(Error)
{
PrintFault(Error,argv[0]);
return(-1);
}
else
return(0);
}